package com.information.manage.gyjc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.gyjc.mapper.GongxuMapper;
import com.information.manage.gyjc.mapper.GongyiMapper;
import com.information.manage.gyjc.mapper.RefGyGxMapper;
import com.information.manage.gyjc.model.enity.Gongxu;
import com.information.manage.gyjc.model.enity.Gongyi;
import com.information.manage.gyjc.model.enity.RefGyGx;
import com.information.manage.gyjc.model.vo.GongxuVo;
import com.information.manage.gyjc.model.vo.GongyiPage;
import com.information.manage.gyjc.model.vo.GongyiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gyjc/gongyi")
public class GongyiController {

    @Autowired
    private GongyiMapper gongyiMapper;
    @Autowired
    private GongxuMapper gongxuMapper;
    @Autowired
    private RefGyGxMapper refGyGxMapper;

    //工艺信息列表
    @GetMapping("/list/{curPage}/{size}")
    public Result selectGongyiList(@PathVariable Integer curPage, @PathVariable Integer size) {
        GongyiPage gongyiPage = new GongyiPage();
        ArrayList<GongyiVo> gongyiVoList = new ArrayList<>();
        Page<Gongyi> page = gongyiMapper.selectPage(new Page<>(curPage, size), null);
        page.getRecords().forEach(item -> {
            ArrayList<GongxuVo> gongxus = new ArrayList<>();
            refGyGxMapper.selectList(new QueryWrapper<RefGyGx>().eq("gy_id", item.getId())).forEach(gx -> {
                Gongxu gongxu = gongxuMapper.selectOne(new QueryWrapper<Gongxu>().eq("id", gx.getGxId()));
                GongxuVo vo = new GongxuVo(gongxu, gx.getGxOrder());
                gongxus.add(vo);
            });
            gongyiVoList.add(new GongyiVo(item, gongxus));
        });
        gongyiPage.setCurrent((int) page.getCurrent());
        gongyiPage.setSize((int) page.getSize());
        gongyiPage.setTotal((int) page.getTotal());
        gongyiPage.setPages((int) page.getPages());
        gongyiPage.setRecords(gongyiVoList);

        return Result.ok(gongyiPage);
    }

    //查询所有工艺
    @GetMapping("/listAll")
    public Result listAll() {
        List<Gongyi> list = gongyiMapper.selectList(null);
        return Result.ok(list);
    }

    //工艺信息详情
    @GetMapping("/select/{id}")
    public Result selectGongyiById(@PathVariable Integer id) {

        Gongyi gongyi = gongyiMapper.selectById(id);
        ArrayList<GongxuVo> gongxus = new ArrayList<>();
        refGyGxMapper.selectList(new QueryWrapper<RefGyGx>().eq("gy_id", gongyi.getId())).forEach(gx -> {
            Gongxu gongxu = gongxuMapper.selectOne(new QueryWrapper<Gongxu>().eq("id", gx.getGxId()));
            gongxus.add(new GongxuVo(gongxu, gx.getGxOrder()));
        });
        // 使用 Stream 进行排序
        List<GongxuVo> sortedGongxus = gongxus.stream()
                .sorted(Comparator.comparing(GongxuVo::getGxOrder))
                .collect(Collectors.toList());
        return Result.ok(new GongyiVo(gongyi, sortedGongxus));
    }

    //工艺信息修改
    @PostMapping("/update")
    public Result<String> updateGongyi(@RequestBody Gongyi gongyi) {
        int res = gongyiMapper.updateById(gongyi);
        if (res > 0) {
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    //工艺信息添加
    @PostMapping("/add")
    public Result<String> insertGongyi(@RequestBody Gongyi gongyi) {
        int res = gongyiMapper.insert(gongyi);
        if (res > 0) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }

    //工艺信息删除
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteGongyi(@PathVariable Integer id) {
        int res = gongyiMapper.deleteById(id);
        if (res > 0) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }
}
